﻿using System;
using System.Collections.Generic;

namespace StationPriority {
    class StationSupplyDemandPair {
        public int StationId { get; }
        public SupplyDemandPairPatch[] Pairs { get; private set; }

        private int pairCount;

        public StationSupplyDemandPair(
            int stationId,
            int capacity
            ) {
            StationId = stationId;
            Pairs = new SupplyDemandPairPatch[capacity];
            pairCount = 0;
        }

        public void SetCapacity(int newCapacity) {
            SupplyDemandPairPatch[] pairs = Pairs;
            Pairs = new SupplyDemandPairPatch[newCapacity];
            Array.Copy(pairs, Pairs, pairs.Length);
        }

        public void Add(
            int supplyId,
            int supplyIndex,
            int supplyPriority,
            int demandId,
            int demandIndex) {
            Pairs[pairCount].origin.supplyId = supplyId;
            Pairs[pairCount].origin.supplyIndex = supplyIndex;
            Pairs[pairCount].supplyPriority = supplyPriority;
            Pairs[pairCount].origin.demandId = demandId;
            Pairs[pairCount].origin.demandIndex = demandIndex;
            pairCount++;
        }

        public void Clear() {
            pairCount = 0;
            Array.Clear(Pairs, 0, Pairs.Length);
        }

        public SupplyDemandPairPatch[] GetSortedPairs() {
            SupplyDemandPairPatch[] sortedPairs = new SupplyDemandPairPatch[pairCount];
            for (int index = 0; index < pairCount; ++index) {
                sortedPairs[index] = Pairs[index];
            }
            Array.Sort(sortedPairs, (pair1, pair2) => pair2.CompareTo(pair1));
            return sortedPairs;
        }
    }
}
